// APP对象
const app = getApp()
// 工具函数
const util = require('../../utils/util')
// animation动画实例
var animationFn = wx.createAnimation({
  transformOrigin: "0 50%",
  duration: 1000,
  timingFunction: "ease",
  delay: 0
})

Page({
  data: {
    flag: false,
    weight: [],
    weightCount: 200,
    leftNum: 60,
    animationFn: {},
    itemWidth: 20,
    sex: 0
  },
  // 页面加载
  onLoad: function (options) {
    // 编辑体重
    if (!util.isEmptyObject(options)) {
      var weight = this.data.leftNum
      var sex = this.data.sex
      // 判断options.sex是否为空
      if ((options.sex != 'null') && (options.sex != 'undefined')) {
        sex = options.sex
      }
      // 判断options.weight是否为null
      if ((options.weight != 'null') && (options.weight != 'undefined')) {
        weight = options.weight
      }
      this.setData({
        leftNum: weight,
        edit: true,
        sex: sex
      })
    } else {
      // 设置性别
      this.setData({
        sex: app.updateUserInfo.sex
      })
    }
  },
  // 页面首次渲染完
  onReady: function () {
    this.init()
  },
  // 初始化
  init: function () {
    var weight = []
    for (var i = 0; i <= this.data.weightCount; i++) {
      var item = {}
      item.weight = i;
      item.lineFlag = (item.weight % 5 === 0) ? true : false
      weight.push(item)
    }
    this.setData({
      weight: weight
    })
    this.setLeft()
  },
  // 监听触屏开始
  touchStart: function (e) {
    this.scrollFlag = true

    var pageX = e.touches[0].pageX
    this.start = {
      distance: this.data.leftNum * this.data.itemWidth,
      x: pageX
    }
  },
  // 监听触屏滚动
  touchMove: function (e) {
    if (!this.scrollFlag) {
      return
    }

    var pageX = e.touches[0].pageX
    var distance = pageX - this.start.x

    this.start.x = pageX
    this.start.distance = this.start.distance - distance
    animationFn.translateX(-this.start.distance).step({ duration: 120 })
    this.setData({
      animationFn: animationFn.export()
    })

    var left = parseInt(this.start.distance / this.data.itemWidth)
    if (left < 0) {
      left = 0
    } else if(left > this.data.weightCount) {
      left = this.data.weightCount
    }

    this.setData({
      leftNum: left
    })
  },
  // 监听触屏结束
  touchEnd: function () {
    var distance = this.start.distance
    var left

    this.scrollFlag = false
    if (distance < 0) {
      distance = 0
    } else if (distance > this.data.weightCount*this.data.itemWidth) {
      distance = this.data.weightCount * this.data.itemWidth
    } else { }

    left = parseInt(distance / this.data.itemWidth)
    console.log(left)
    this.setData({
      leftNum: left
    })

    this.setLeft()
  },
  // 设置滚动距离
  setLeft: function () {
    var left = this.data.leftNum * this.data.itemWidth
    animationFn.translateX(-left).step()
    this.setData({
      animationFn: animationFn.export()
    })
  },
  // 点击上一步
  tapGoBack: function () {
    wx.navigateBack()
  },
  // 点击下一步
  tapGoNext: function () {
    var weight = this.data.weight[this.data.leftNum]['weight']
    // 编辑体重
    if (this.data.edit) {
      // 获取前一页数据
      var userInfo = util.getPrevPageData('userInfo')
      userInfo.weight = weight
      // 修改前一页数据
      util.editPrevPageData({
        userInfo: userInfo
      })
      // 设置本页数据
      this.setData({
        weight: weight
      })
      wx.navigateBack()
    } else {
      // 初始化用户资料选择体重
      app.updateUserInfo.weight = weight
      wx.navigateTo({
        url: '/pages/loginStep5/loginStep5',
      })
    }
  }
})